% Copyright 2018  李文威 (Wen-Wei Li).
% Permission is granted to copy, distribute and/or modify this
% document under the terms of the Creative Commons
% Attribution 4.0 International (CC BY 4.0)
% http://creativecommons.org/licenses/by/4.0/

% 目的: 设置章节标题格式及目录的显示方式.
% 由 AJbook.cls 引入
\ProvidesFile{titles-setup.tex}[2018/03/04]

\RequirePackage[calcwidth, nobottomtitles, explicit, newparttoc, indentafter]{titlesec}      % 标题格式: explicit 选项导致须在 titleformat 的 before-code 中加 #1. 选项 newparttoc 用来将各部分加入目录. indentafter: 首行一律缩进
\RequirePackage{titletoc}                 % 目录格式

% 目录部分: 章名除附录外仍用中文标号, 黑体显示. 以参数是否为大写拉丁字母来判定是否在附录 (烂招)
\if@AJ@traditional
	\providecommand{\AJchapterttl}[1]{\IfSubStr{ABCDEFGHIJKLMNOPQRSTUVWXYZ}{#1}{附錄 #1	}{第\CJKnumber{#1}章}}
\else
	\providecommand{\AJchapterttl}[1]{\IfSubStr{ABCDEFGHIJKLMNOPQRSTUVWXYZ}{#1}{附录 #1	}{第\CJKnumber{#1}章}}
\fi

\newlength{\BoxTtlwidth}	% 用来计算各种盒子所需宽度

% 各章标题排版: \MakeChapBox{编号}{标题}, 除附录外, 各章用中文数字显示. 以参数是否为大写拉丁字母来判定是否在附录 (烂招)
\newcommand{\MakeChapBox}[2]{%
	\settowidth{\BoxTtlwidth}{\AJchapterttl{#1}} % 计算宽度
	\begin{tcolorbox}[ % 设置 tcolorbox
		enhanced jigsaw,
		skin = bicolor,
		frame engine = path,
		sharp corners = all,
		width = 0.9\textwidth,
		top = 4mm, bottom = 4mm,
		sidebyside,
		frame hidden,
		boxrule = 0mm,
		lefthand width = \BoxTtlwidth,
		colupper = white,
		colback = gray!80,
		colbacklower = gray!10,
		sidebyside align=center,
		halign=center]
		\AJchapterttl{#1}
		\tcblower
		#2
	\end{tcolorbox}%
}

% 无编号标题排版: \MakeChapBox{标题}
\newcommand{\MakeChapBoxSingle}[1]{%
	\begin{tcolorbox}[
		enhanced,
		width = 0.7\textwidth,
		sharp corners = all,
		top = 4mm, bottom = 4mm,
		frame hidden,
		boxrule = 0mm,
		colback = gray!10,
		halign=center]
		#1
	\end{tcolorbox}
}

% 各节标题排版: \MakeSectBox{文字}, 问题: 不允许断行
\newtcbox{\MakeSectBox}{
	enhanced,
	arc = 0pt, outer arc = 0pt,
	before skip = 0pt, after skip = 0.4em, left skip = 0pt, right skip = 0pt,
	top = 0pt, left = 0pt, right = 0pt, bottom = 1.5mm,
	sharp corners = all,
	valign=bottom,
	colback = white,
	colframe = white,
	boxsep = 0pt, leftrule = 0pt, rightrule=0pt, toprule=0pt, bottomrule = 0pt,
	overlay = { \draw[line width=1pt] (interior.south west) -- (interior.south east); }
}

% 用 titlesec 设置各章标题
\titleformat{name=\chapter}
	{\filright\sffamily\CJKfamily{hei2}\bfseries\Huge}	% Format
	{}	% Label
	{0mm}	% Sep
	{\MakeChapBox{\thechapter}{#1}}	% Before-code
	[]	% After-code
\titlespacing*{name=\chapter}	% 设置间隔
	{1pc}{*4}{1em}	% {left}{before-sep}{after-sep}

\titleformat{name=\chapter, numberless}
	{\filcenter\sffamily\CJKfamily{hei2}\bfseries\Huge}	% Format
	{}	% Label
	{0mm}	% Sep
	{\MakeChapBoxSingle{#1}}	% Before-code
	[{\if@mainmatter
		\addcontentsline{toc}{chapter}{#1}
		\markboth{#1}{}
	\fi}]	% After-code: 无号章如果出现在正文中, 就加入目录并相应地设置天眉.
\titlespacing*{name=\chapter, numberless}	% 设置间隔
	{1pc}{*4}{1em}	% {left}{before-sep}{after-sep}

\titleformat{name=\section} % 各节标题
	{\filleft\normalfont\sffamily\bfseries\CJKfamily{sectionfont}}
	{}
	{0mm}
	{ \settowidth{\BoxTtlwidth}{\Huge \thesection \hspace{0.7em} \Large #1}  % 首先计算宽度
		\ifdim \BoxTtlwidth < \textwidth % 一般情形下调用 \MakeSectBox
			\MakeSectBox{\Huge \thesection \hspace{0.7em} \Large #1}\vskip-18pt%
		\else % 万一标题过长则不用 \MakeSectBox (烂招)
			\Huge \underline{\thesection} \hspace{0.7em} \Large #1%
		\fi %
	}
	[]

\titleformat{name=\section, numberless}	% 各节标题: 无编号情形
	{\filleft\normalfont\sffamily\bfseries\CJKfamily{sectionfont}}
	{}
	{0mm}
	{ \settowidth{\BoxTtlwidth}{\Large #1}  % 首先计算宽度
		\ifdim \BoxTtlwidth < \textwidth % 一般情形下调用 \MakeSectBox
			\MakeSectBox{\Large #1}\vskip-18pt%
		\else % 万一标题过长则不用 \MakeSectBox (烂招)
			\Large #1 %
		\fi %
	}
	[]
	
\titlespacing*{name=\section}	% 设置间隔
	{1pc}{*1.3}{*1}    % {left}{before-sep}{after-sep}

\titleformat{name=\subsection} % 各子节标题, 采取 runin 形式较美观
	[runin]
	{\filleft\normalfont\sffamily\bfseries\CJKfamily{sectionfont}}
	{\Large\thesubsection}	% Label
	{3mm}	% Sep
	{#1}	% Before-code
	[]		% After-code

\titleformat{name=\subsection, numberless}
	[runin]
	{\filleft\normalfont\sffamily\bfseries\CJKfamily{sectionfont}}
	{}	% Label
	{0mm}	% Sep
	{#1}	% Before-code
	[]		% After-code
\titlespacing*{name=\subsection}	% 设置间隔
	{0pt}{*1}{1em}    % {left}{before-sep}{after-sep}

\titleformat{name=\subsubsection} % 次子节标题, 采取 runin 形式较美观
	[runin]
	{\filleft\normalfont\sffamily\bfseries\CJKfamily{sectionfont}}
	{\thesubsubsection}	% Label
	{3mm}	% Sep
	{#1}	% Before-code
	[]		% After-code
	
\titleformat{name=\subsubsection, numberless}
	[runin]
	{\filleft\normalfont\sffamily\bfseries\CJKfamily{sectionfont}}
	{}	% Label
	{0mm}	% Sep
	{#1}	% Before-code
	[]		% After-code

\titlespacing*{name=\subsubsection}	% 设置间隔
	{0pt}{*1}{1em}    % {left}{before-sep}{after-sep}

\renewcommand{\thepart}{\CJKnumber{\arabic{part}}}	% 部分的标题编号汉化
\titleformat{name=\part}[display]	% 各部分标题
	{\filcenter\sffamily\bfseries\CJKfamily{song}\Huge}	% Format
	{第{\thepart}部分}	% Label
	{1.5em}	% Sep
	{#1}	% Before-code
	[]	% After-code

\titlecontents{chapter}
	[0pt]
	{\addvspace{1pc}\heiti}
	{\contentsmargin{0pt}\large\AJchapterttl{\thecontentslabel} \quad}
	{\contentsmargin{0pt}\large}
	{\titlerule*[.7pc]{.}\contentspage}

\titlecontents{section}
	[1.5em]
	{}
	{\thecontentslabel\quad}
	{\thecontentslabel}
	{\titlerule*[.7pc]{.}\contentspage}

\titlecontents{subsection}
	[3.9em]
	{\small}
	{\thecontentslabel\quad}
	{\ (\thecontentspage)}
	{\titlerule*[.7pc]{.}\contentspage}

\titlecontents{part}
	[0cm]
	{\addvspace{1pc}\songti}
	{\contentsmargin{0pt}\large{第{\thecontentslabel}部分}\quad}
	{\large}
	{\titlerule*[.7pc]{.}\contentspage}
